package groupon.com.bo.controller;

import groupon.user.bo.service.AccountService;
import groupon.user.model.Account;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/auth")
public class LoginLogoutController {
	
	private final static Log log = LogFactory.getLog(LoginLogoutController.class);
	
	@Autowired
	private AccountService _accountService;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String getLoginPage(@RequestParam(value ="error", required = false) boolean error, ModelMap model){
		log.debug("login page");
		if(error){
			model.put("error","You have entered an invalid username or password!");
		}else{
			model.put("error", "");
		}
		return "/login";
	}
	
	@RequestMapping(value = "denied" , method = RequestMethod.GET)
	public String getDeniedPage(){
		return "/bo/deniedpage";
	}
	
	@RequestMapping(value = "/forgetPw", method = RequestMethod.GET)
	public void forgetPw(){
	}
	
	@RequestMapping(value = "/submitForgetPw", method = RequestMethod.POST)
	public String submitForgetPw(@RequestParam String email, ModelMap model){
		Account account = _accountService.findAccountbyEmail(email);
		if(account == null){
			model.addAttribute("error", "dont find see email,pls try it again");
		}else{
			//todo change pw for email and send email
//			AppUtil.sendEmail();
			model.addAttribute("error", "your password was change.pls check email");
		}
		return "/forgetPw";
	}
}
